﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Collections.ObjectModel;
using System.Security.Principal;
using VisualAgile.Model;

namespace VisualAgile.Controllers
{
    public class ActiveUser : IPrincipal
    {
        public static ActiveUser Guest
        {
            get
            {
                return new ActiveUser() { Email = "Guest@guest.com", Role = Role.Guest };
            }
        }

        public string PublicIdentity
        {
            get
            {
                if (CanLogin) return Email;
                return "Guest";
            }
        }

        public bool CanLogin { get { return IsAdministrator || IsLeadDeveoper; } }        

        public IIdentity Identity
        {
            get
            {
                bool isAuthenticated = !(Role.Name == Role.Guest.Name);
                return new Identity(isAuthenticated, this.Email);
            }
        }

        public bool IsInRole(string role)
        {
            return this.Role.Name == role;
        }
       
        public bool IsAdministrator { get { return Role.RoleID == Role.AdministratorId; } }
        public bool IsLeadDeveoper { get { return Role.RoleID == Role.LeadDeveloperId; } }
        public bool IsDeveloper { get { return Role.RoleID == Role.DeveloperId; } }

        /// <summary>
        /// Email地址
        /// </summary>
        public System.String Email
        {
            get;
            set;
        }

        /// <summary>
        /// User role
        /// </summary>
        /// 
        public Role Role
        {
            get;
            set;
        }

        /// <summary>
        /// 手机号码
        /// </summary>
        public System.String Mobile
        {
            get;
            set;
        }

        /// <summary>
        /// 用户密码
        /// </summary>
        public System.String Password
        {
            get;
            set;
        }

        /// <summary>
        /// 电话号码
        /// </summary>
        public System.String Telephone
        {
            get;
            set;
        }

        /// <summary>
        /// 用户名称
        /// </summary>
        public System.String UserName
        {
            get;
            set;
        }      
    }

    /// <summary>
    /// Simple implementation of IIdentity
    /// </summary>
    public class Identity : IIdentity
    {
        bool isAuthenticated;
        string name;

        public Identity(bool isAuthenticated, string name)
        {
            this.isAuthenticated = isAuthenticated;
            this.name = name;
        }

        public string AuthenticationType
        {
            get { return "Forms"; }
        }

        public bool IsAuthenticated
        {
            get { return isAuthenticated; }
        }

        public string Name
        {
            get { return name; }
        }
    }   
}
